[#ftl]
[@define "agile._ContainerToolbar2"]
	constructor:function(){
	}	
 	,init:function(){
		////console.log(this.declaredClass,"[agile._ContainerToolbar2]init...");
		this.initToolbar();			
		this.postInitToolbar();	
		aspect.after(this,"onChildAdded",lang.hitch(this,"_adjustChildOffset"),true);
	}
	,initToolbar:function(){

		this.removeButton = construct.create("div",{innerHTML:"<img class='agileButton agileButtonRemove2'></img>"}).firstChild;
		this.addButton = construct.create("div",{innerHTML:"<img class='agileButton agileButtonAdd2'></img>"}).firstChild;
		
		aspect.after(this.removeButton,"onclick",lang.hitch(this,function(event){				
			event.stop();				
			this.removeChild(this.selectedChild);
		}),true);
		
		aspect.after(this.addButton,"onclick",lang.hitch(this,function(event){				
			//event.stop();
			var child = this.createChild();
			this.addComponent(child,this.getOffset(child));
		}),true);
		
		construct.place(this.addButton,this.domNode,"last");
	}
	,postInitToolbar:function(){
		
	}
	,getOffset:function(child){
		return null;
	}
	,_adjustChildOffset:function(child){		
		var children = this.getChildren();		
		
		if(this.sortAxis == "y"){		
			var maxTop = 0;
			var top = 0;
			var childx = null;
			for(var i = children.length - 1; i >= 0; i--){
				top = children[i].dimension.top;
				if(!isNaN(top) && maxTop < top){
					maxTop = top;
					childx = children[i];
				}
			}
			child.metadata("style.top",maxTop == 0? 10 : maxTop + childx.getPreferredSize().h);
			style.set(child.domNode,"top",child.metadata("style.top"));
			
		}else if(this.sortAxis == "x"){			
			var maxLeft = 0;
			var left = 0;
			var childx = null;
			for(var i = children.length - 1; i >= 0; i--){
				//left = children[i].metadata("style.left");
				left = children[i].dimension.left;
				if(!isNaN(left) && maxLeft < left){
					maxLeft = left;
					childx = children[i];
				}
			}
			child.metadata("style.left",maxLeft == 0? 10 : maxLeft + childx.getPreferredSize().w);
			style.set(child.domNode,"left",child.metadata("style.left"));			
		}			
	}
[/@]
